package com.etoak.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.etoak.api.DishesService;
import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.OrderItem;
import com.etoak.mapper.OrderItemMapper;
import com.etoak.service.OrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etoak.vo.DishesVO;
import com.etoak.vo.OrderDetailVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author etoak
 * @since 2025-07-31
 */
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements OrderItemService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    DishesService dishesService;

    @Override
    public List<OrderDetailVO> getDetail(String orderNo) {
        List<OrderItem> itemList = lambdaQuery().eq(OrderItem::getOrderNo, orderNo).list();
        List<Integer> dishesIdList = itemList.stream().map(OrderItem::getDishesId).toList();

        ResultVO<List<DishesVO>> listResultVO = dishesService.get(dishesIdList);

        Map<Integer,Integer> dishesIdMap = new HashMap<>();
        itemList.forEach(item ->{
            dishesIdMap.put(item.getDishesId(), item.getDishesId());
        });

        List<OrderDetailVO> listVO = new ArrayList<>();
        if (listResultVO.getCode().equals(ResultVO.SUCCESS_CODE) && CollectionUtil.isNotEmpty(listResultVO.getData())) {
            listVO = listResultVO.getData().stream().map(d -> {
                OrderDetailVO orderDetailVO = new OrderDetailVO();
                orderDetailVO.setOrderNo(orderNo);
                orderDetailVO.setDishesName(d.getName());
                orderDetailVO.setDishesNum(dishesIdMap.get(d.getId()));
                StringBuffer detail = new StringBuffer();
                detail.append(d.getMainName()).append("；").append(d.getMainNum()).append("g")
                        .append(d.getMinorName()).append("；").append(d.getMinorNum()).append("g")
                        .append(d.getSeasoningName()).append("；").append(d.getSeasoningNum()).append("g");
                orderDetailVO.setDetail(detail.toString());
                return orderDetailVO;
            }).toList();
        }


        return listVO;
    }
}
